package com.djinfo.work.activiti.service.impl;

import com.djinfo.work.activiti.dao.PermissionDao;
import com.djinfo.work.activiti.dao.RolesDao;
import com.djinfo.work.activiti.dao.UsersDao;
import com.djinfo.work.activiti.po.*;
import com.djinfo.work.activiti.service.SystemService;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.entity.TbSysRoleUser;
import com.djinfo.work.entity.TbSysRoles;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT,timeout=5)
@Service("SystemService")
public class SystemServiceImpl extends BaseDaoImpl implements SystemService{
	@Autowired
	UserService userService;
	@Autowired
	UsersDao userDao;
	@Autowired
	RolesDao roleDao;
	@Autowired
	PermissionDao permissionDao;
	//修改过
	public List<TbSysUsers> getpageusers(int pagenum, int pagesize) {
		List<TbSysUsers> l=userDao.getallTbSysUsers(pagenum,pagesize);
		return l;
	}

	@Override
	public List<TbSysUsers> getTbSysUsers() {
		return userDao.getTbSysUsers();
	}

	//修改过
	public TbSysUsers getUserByid(String id) {
		TbSysUsers u=userDao.getTbSysUsersbyid(id);
		return u;
	}
	//修改
	public List<TbSysRoles> getpageRoleinfo(int pagenum, int pagesize) {
		List<TbSysRoles> l=roleDao.getRoleinfo(pagenum,pagesize);
		return l;
	}

	@Override
	public List<TbSysRoles> getRoleinfo() {
		return null;
	}

	//修改
	public List<TbSysRoles> getRoleinfo(int pagenum, int pagesize) {
		return roleDao.getRoleinfo(pagenum,pagesize);
	}
	public List<Permission> getPermisions() {
		return permissionDao.getallPermission();
	}

	public void deleterolepermission(String rid) {
		roleDao.deleterolepermission(rid);
	}


	public void updaterole(String rid, String[] permissionnames) {
		TbSysRoles tbSysRoles = roleDao.getTbSysRolesbyid(rid);
		for(String permissionname:permissionnames){
			Permission p= permissionDao.getpermissionbypermissionname(permissionname);
			Role_permission rp=new Role_permission();
			rp.setRoleid(tbSysRoles.getPkRoleId());
			rp.setPermissionid(p.getPid());
			permissionDao.addrole_permission(rp);
		}
	}
	public List<Permission> getPagePermisions(int pagenum, int pagesize) {
		PageHelper.startPage(pagenum,pagesize);  
		return permissionDao.getallPermission();
	}
	public void addPermission(String permissionname) {
		permissionDao.addpermission(permissionname);
	}

	public void deletepermission(String pid) {
		Permission permission = permissionDao.getpermissionbyid(pid);
		permissionDao.delRole_permission(pid);
		permissionDao.delpermission(permission);

	}
	//修改过
	public String getUidByusername(String username) {
		String id = userDao.getUidByusername(username);
		return id;
	}

	@Override
	public void deleterole(String rid) {
		roleDao.deleterole(rid);
	}

	@Override
	public void addPermission(Permission permission) {
		permissionDao.addpermi(permission);
	}

	@Override
	public void addSysRoles(TbSysRoles tbSysRoles, String[] permissionnames) {
		this.save(tbSysRoles);
		for(String permissionname:permissionnames){
			Permission p = this.findUniqueByProperty(Permission.class,"permissionname",permissionname);
			Role_permission rp=new Role_permission();
			rp.setRoleid(tbSysRoles.getPkRoleId());
			rp.setPermissionid(p.getPid());
			this.save(rp);
		}
	}

	//新增
	@Override
	public List<TbSysRoleUser> getTbSysRoleUser(String id) {
		List<TbSysRoleUser> tbSysRoleUserList = roleDao.getTbSysRoleUser(id);
		return tbSysRoleUserList;
	}
	//新增
	@Override
	public TbSysRoles getTbSysRolesbyid(String id) {
		return roleDao.getTbSysRolesbyid(id);
	}
	//新增
	@Override
	public List<TbSysUsers> getallTbSysUsers(int pagenum, int pagesize) {
		return userDao.getallTbSysUsers(pagenum, pagesize);
	}
	@Override
	public List<TbSysRoles> getallRoleinfo() {
		return roleDao.getallRoleinfo();
	}


}
